package com.wzapp.sitposture.common.util;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * @author ql
 */
public class OkHttpUtil {

    private static final Logger log = LoggerFactory.getLogger(OkHttpUtil.class);

    /**
     * 获取Request
     *
     * @param url
     * @return
     */
    private static Request getRequest(String url) {

        return new Request.Builder()
                .url(url)
                .get().addHeader("Content-Type", "application/json")
                .build();
    }

    /**
     * 获取Request
     *
     * @param url
     * @return
     */
    private static Request patchRequest(String url, RequestBody body) {

        return new Request.Builder()
                .url(url)
                .patch(body).addHeader("Content-Type", "application/json")
                .build();
    }

    /**
     * 获取Request
     *
     * @param url
     * @return
     */
    private static Request deleteRequest(String url) {

        return new Request.Builder()
                .url(url)
                .delete().addHeader("Content-Type", "application/json")
                .build();
    }

    private static Request deleteRequest(String url, RequestBody body) {

        return new Request.Builder()
                .url(url)
                .delete(body).addHeader("Content-Type", "application/json")
                .build();
    }

    /**
     * 获取Request
     *
     * @param url
     * @return
     */
    private static Request postRequest(String url, RequestBody body) {

        return new Request.Builder()
                .url(url)
                .post(body).addHeader("Content-Type", "application/json")
                .build();
    }

    /**
     * 执行get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static Response executeGet(String url) throws IOException {

        log.info(url);
        Request request = getRequest(url);
        OkHttpClient client = new OkHttpClient();
        return client.newCall(request).execute();
    }

    /**
     * 执行get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static Response executePatch(String url, RequestBody body) throws IOException {

        log.info(url);
        Request request = patchRequest(url, body);
        OkHttpClient client = new OkHttpClient();
        return client.newCall(request).execute();
    }

    /**
     * 执行get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    public static Response executePost(String url, RequestBody body) throws IOException {

        log.info(url);
        Request request = postRequest(url, body);
        OkHttpClient client = new OkHttpClient();
        return client.newCall(request).execute();
    }

    public static Response executeDelete(String url) throws IOException {

        Request request = deleteRequest(url);
        OkHttpClient client = new OkHttpClient();
        return client.newCall(request).execute();
    }

    public static Response executeDelete(String url, RequestBody body) throws IOException {

        Request request = deleteRequest(url, body);
        OkHttpClient client = new OkHttpClient();
        return client.newCall(request).execute();
    }

    /**
     * 获取ResponseEntityH
     *
     * @param response
     * @return
     */
    public Object obtainResponseJson(Response response) throws IOException {

        int httpCode = response.code();
        String resultStr = response.body().string();

        log.error("httpCode" + httpCode + "/r/n" + resultStr);
        if (!response.isSuccessful()) {
            log.error("httpCode" + httpCode + "平台返回值异常==" + resultStr);
        }

        return  resultStr;
    }
}
